﻿using DG.Tweening;
using ScFramework.Core;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CameraManager:SystemInstance<CameraManager>
{
    private Camera camera = null;

    private Vector2 siz;
    public Vector2 ViewSiz
    {
        get
        {
            return siz;
        }
    }
    public Vector2 CameraPos
    {
        get
        {
            return camera.transform.position;
        }
    }
    public Camera eCamera = null;
    Transform Texture = null;
    public override void Initialize(Main m)
    {
        base.Initialize(m);
        camera =new List<Camera>( GameObject.FindObjectsOfType<Camera>()).Find(s=>s.name=="Camera");
        eCamera = new List<Camera>(GameObject.FindObjectsOfType<Camera>()).Find(s => s.name.StartsWith("Effect"));
        Texture = GameObject.Find("MeshWithTextureFromCamera").transform;
        siz = camera.ScreenToWorldPoint(new Vector3(camera.pixelWidth, camera.pixelHeight)) - camera.ScreenToWorldPoint(Vector3.zero);
    //    Debug.Log(siz);
    }
    public override void Update(float deltaTime)
    {
        base.Update(deltaTime);
        if (InstanceManager.player != null)
        {
            var pos = InstanceManager.player.transform.position;
            pos.z = camera.transform.position.z;
            camera.transform.DOMove(pos, Main.Instance.CameConf.posDur*Player.Instance.Scale);
            var angle = MathTool.VectorToAngle(InstanceManager.player.DownVec) + 90f;
            var rot = new Vector3(0, 0, angle);
            camera.transform.DORotate(rot, Main.Instance.CameConf.rotDur* Player.Instance.Scale);
            eCamera.transform.position = camera.transform.position;
            eCamera.transform.rotation = Texture.rotation = camera.transform.rotation;
            pos = camera.transform.position;
            pos.z = Texture.transform.position.z;
            Texture.transform.position = pos;
        }
     //   Debug.Log(Input.mousePosition);
    }

}
[Serializable]
public class CameraConfig
{
    public float posDur = 1;
    public float rotDur = 1;

}